package io.yuncheng.modules.sys.service.impl;

import io.yuncheng.common.utils.Constant;
import io.yuncheng.modules.sys.entity.SubApplicationEntity;
import io.yuncheng.modules.sys.service.SubApplicationService;
import io.yuncheng.modules.sys.service.SubAppAssessmentSplitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.yuncheng.common.utils.PageUtils;
import io.yuncheng.common.utils.Query;

import io.yuncheng.modules.sys.dao.SubAppAssessmentSplitDao;
import io.yuncheng.modules.sys.entity.SubAppAssessmentSplitEntity;


@Service("subAppAssessmentSplitService")
public class SubAppAssessmentSplitServiceImpl extends ServiceImpl<SubAppAssessmentSplitDao, SubAppAssessmentSplitEntity> implements SubAppAssessmentSplitService {
    @Autowired
    private SubApplicationService applicationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SubAppAssessmentSplitEntity> page = this.page(
                new Query<SubAppAssessmentSplitEntity>().getPage(params),
                new QueryWrapper<SubAppAssessmentSplitEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SubAppAssessmentSplitEntity> queryListByDeptYear(Long deptId,Integer year,Integer status) {
        SubApplicationEntity appId = applicationService.queryByDeptId(deptId);
        if (appId == null) {
            appId = new SubApplicationEntity();
            appId.setAppId(-1L);
        }
        Date date = null;
        if(year!=null){
            date =  new Date(year - 1900, 0, 1);
        }
        return  queryListByEntity(appId.getAppId(),date,status);
    }
    @Override
    public List<SubAppAssessmentSplitEntity> queryListByEntity(Long appId,Date  date,Integer status) {
        QueryWrapper<SubAppAssessmentSplitEntity> sub = new QueryWrapper<>();
        SubAppAssessmentSplitEntity entity = new SubAppAssessmentSplitEntity();
        entity.setAppId(appId);
        entity.setAssessmentDate(date);
        entity.setStatus(status);
        sub.setEntity(entity);
        sub.gt("split_type_three",3);
        sub.orderByAsc("split_type_three");
        sub.orderByAsc("assessment_date");
        return this.list(sub);
    }

    @Override
    public List<SubAppAssessmentSplitEntity> queryListByEntityAll(Long appId, Date date) {
        return list(buildByIdAndDate(appId,date));
    }

    @Override
    public List<SubAppAssessmentSplitEntity> queryListByEntityAllByAppId(Long appId) {
        QueryWrapper<SubAppAssessmentSplitEntity> sub = new QueryWrapper<>();
        SubAppAssessmentSplitEntity entity = new SubAppAssessmentSplitEntity();
        entity.setAppId(appId);
        entity.setStatus(Constant.StatusType.NORMAL.getValue());
        sub.setEntity(entity);
        return list(sub);
    }

    @Override
    public Boolean notHasData(Long appId,Date  date) {
        int one = this.count(buildByIdAndDate(appId,date));
        return one <= 0;
    }
    private QueryWrapper<SubAppAssessmentSplitEntity> buildByIdAndDate(Long appId,Date  date){
        QueryWrapper<SubAppAssessmentSplitEntity> sub = new QueryWrapper<>();
        SubAppAssessmentSplitEntity entity = new SubAppAssessmentSplitEntity();
        entity.setAppId(appId);
        entity.setAssessmentDate(date);
        entity.setStatus(Constant.StatusType.NORMAL.getValue());
        sub.setEntity(entity);
        return sub;
    }
    @Override
    public List<SubAppAssessmentSplitEntity> queryNormalListByEntityNum(Long appId) {
        QueryWrapper<SubAppAssessmentSplitEntity> sub = new QueryWrapper<>();
        SubAppAssessmentSplitEntity entity = new SubAppAssessmentSplitEntity();
        entity.setAppId(appId);
        entity.setStatus(Constant.StatusType.NORMAL.getValue());
        sub.setEntity(entity);
        sub.in("split_type_three", Arrays.asList(2,3));
        return list(sub);
    }

}
